/*
 * DexProto.h
 *
 *  Created on: 2011-7-16
 *      Author: gexiao
 */
/*
 * Functions for dealing with method prototypes
 */
#ifndef DEXPROTO_H_
#define DEXPROTO_H_
#include "DexFile.h"

/*
 * Method prototype structure, which refers to a protoIdx in a
 * particular DexFile.
 */
typedef struct DexProto {
	const DexFile* dexFile;//file the idx refers to
	u4 protoIdx;//index into proto_ids table of dexFile
} DexProto;

/*
 * Set the given DexProto to refer to the prototype of the given MethodId.
 */DEX_INLINE void dexProtoSetFromMethodId(DexProto* pProto,
		const DexFile* pDexFile, const DexMethodId* pMethodId) {
	pProto->dexFile = pDexFile;
	pProto->protoIdx = pMethodId->protoIdx;
}

/*
 * Get the short-form method descriptor for the given prototype. The
 * prototype must be protoIdx-based.
 */
const char* dexProtoGetShorty(const DexProto* pProto);

/*
 * Get the type descriptor for the return type of the given prototype.
 */
const char* dexProtoGetReturnType(const DexProto* pProto);

/*
 * Get the parameter count of the given prototype.
 */
size_t dexProtoGetParameterCount(const DexProto* pProto);

/*
 * Compute the number of parameter words (u4 units) required by the
 * given prototype. For example, if the method takes (int,long) and
 * returns double, this would return 3 (one for the int, two for the
 * long, and the return type isn't relevant).
 */
int dexProtoComputeArgsSize(const DexProto* pProto);

/*
 * Compare the two prototypes. The two prototypes are compared
 * with the return type as the major order, then the first arguments,
 * then second, etc. If two prototypes are identical except that one
 * has extra arguments, then the shorter argument is considered the
 * earlier one in sort order (similar to strcmp()).
 */
int dexProtoCompare(const DexProto* pProto1, const DexProto* pProto2);

/*
 * Compare a prototype and a string method descriptor. The comparison
 * is done as if the descriptor were converted to a prototype and compared
 * with dexProtoCompare().
 */
int dexProtoCompareToDescriptor(const DexProto* proto, const char* descriptor);

/*
 * Single-thread prototype parameter iterator. This structure holds a
 * pointer to a prototype and its parts, along with a cursor.
 */
typedef struct DexParameterIterator {
	const DexProto* proto;
	const DexTypeList* parameters;
	int parameterCount;
	int cursor;
} DexParameterIterator;

/*
 * Initialize the given DexParameterIterator to be at the start of the
 * parameters of the given prototype.
 */
void dexParameterIteratorInit(DexParameterIterator* pIterator,
		const DexProto* pProto);

/*
 * Get the type_id index for the next parameter, if any. This returns
 * kDexNoIndex if the last parameter has already been consumed.
 */
u4 dexParameterIteratorNextIndex(DexParameterIterator* pIterator);

/*
 * Get the type descriptor for the next parameter, if any. This returns
 * NULL if the last parameter has already been consumed.
 */
const char* dexParameterIteratorNextDescriptor(DexParameterIterator* pIterator);

#endif /* DEXPROTO_H_ */
